package main

import (
	"fmt"
	"gee"
	"html/template"
	"log"
	"net/http"
	"time"
)

// func indexHandler(w http.ResponseWriter, r *http.Request) {
// 	fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
// }

// func hellowhandle(w http.ResponseWriter, r *http.Request) {
// 	for k, v := range r.Header {
// 		fmt.Fprintf(w, "header[%q]=%q\n", k, v)
// 	}
// }

// func test0() {
// 	r := gee.New()
// 	r.GET("/", func(w http.ResponseWriter, r *http.Request) {
// 		fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
// 	})
// 	r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
// 		for k, v := range r.Header {
// 			fmt.Fprintf(w, "head[%q] = %q", k, v)
// 		}
// 	})
// 	r.Run(":9999")
// }
// func test2(){
// 	s := "<h1>Hello Gee</h1>"
// 	tmpl, err := template.New("name").Parse(s)
// 	err = tmpl.Execute(os.Stdout,)
// }

func test3() {
	netWeb := gee.New()
	// netWeb.GET("/", func(context *gee.Context) {
	// 	context.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
	// })
	netWeb.GET("/hello", func(c *gee.Context) {
		// expect /hello?name=geektutu
		c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
	})
	netWeb.POST("/login", func(c *gee.Context) {
		c.Json(http.StatusOK, map[string]any{
			"username": c.PostForm("username"),
			"password": c.PostForm("password"),
		})
	})
	netWeb.Run(":9999")
}

func newTestRouter() *gee.Engine {
	netWeb := gee.New()
	netWeb.GET("/", nil)
	netWeb.GET("/hello/:name", nil)
	netWeb.GET("/hello/b/c", nil)
	netWeb.GET("/assets/*filepath", nil)
	netWeb.GET("/hi/:name", nil)
	return netWeb
}
func test4() {
	r := gee.New()
	// r.GET("/", func(c *gee.Context) {
	// 	c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
	// })

	r.GET("/hello", func(c *gee.Context) {
		// expect /hello?name=geektutu
		c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
	})

	r.GET("/hello/:name", func(c *gee.Context) {
		// expect /hello/geektutu
		c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
	})

	r.GET("/assets/*filepath", func(c *gee.Context) {
		c.Json(http.StatusOK, map[string]any{"filepath": c.Param("filepath")})
	})

	r.Run(":9999")
}
func test5() {
	r := gee.New()
	// r.GET("/index", func(c *gee.Context) {
	// 	c.HTML(http.StatusOK, "<h1>Index Page</h1>")
	// })
	v1 := r.Group("/v1")
	{
		// v1.GET("/", func(c *gee.Context) {
		// 	c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
		// })

		v1.GET("/hello", func(c *gee.Context) {
			// expect /hello?name=geektutu
			c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
		})
	}
	v2 := r.Group("/v2")
	{
		v2.GET("/hello/:name", func(c *gee.Context) {
			// expect /hello/geektutu
			c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
		})
		v2.POST("/login", func(c *gee.Context) {
			c.Json(http.StatusOK, gee.H{
				"username": c.PostForm("username"),
				"password": c.PostForm("password"),
			})
		})

	}

	r.Run(":9999")
}

func onlyForV2() gee.HandleFunc {
	return func(c *gee.Context) {
		// Start timer
		t := time.Now()
		// if a server error occurred
		c.String(500, "Internal Server Error")
		// Calculate resolution time
		log.Printf("[%d] %s in %v for group v2", c.StatusCode, c.Req.RequestURI, time.Since(t))
	}
}

func test6() {
	r := gee.New()
	r.Use(gee.Logger()) // global midlleware
	// r.GET("/", func(c *gee.Context) {
	// 	c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
	// })

	v2 := r.Group("/v2")
	v2.Use(onlyForV2()) // v2 group middleware
	{
		v2.GET("/hello/:name", func(c *gee.Context) {
			// expect /hello/geektutu
			c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
		})
	}

	r.Run(":9999")
}

func test7() {
	r := gee.New()
	r.StaticFileSource("/assets", "E:/GoCode/Gee")
	// 或相对路径 r.Static("/assets", "./static")
	r.Run(":9999")
}

type student struct {
	Name string
	Age  int8
}

func FormatAsDate(t time.Time) string {
	year, month, day := t.Date()
	return fmt.Sprintf("%d-%02d-%02d", year, month, day)
}

func test8() {
	r := gee.New()
	r.Use(gee.Logger())
	r.SetFuncMap(template.FuncMap{
		"FormatAsDate": FormatAsDate,
	})
	r.LoadHTMLGlob("templates/*")
	r.StaticFileSource("/assets", "./static")

	stu1 := &student{Name: "Geektutu", Age: 20}
	stu2 := &student{Name: "Jack", Age: 22}
	r.GET("/", func(c *gee.Context) {
		c.HTML(http.StatusOK, "css.tmpl", nil)
	})
	r.GET("/students", func(c *gee.Context) {
		c.HTML(http.StatusOK, "arr.tmpl", gee.H{
			"title":  "gee",
			"stuArr": [2]*student{stu1, stu2},
		})
	})

	r.GET("/date", func(c *gee.Context) {
		c.HTML(http.StatusOK, "custom_func.tmpl", gee.H{
			"title": "gee",
			"now":   time.Date(2019, 8, 17, 0, 0, 0, 0, time.UTC),
		})
	})

	r.Run(":9999")
}

func test9() {
	r := gee.Default()
	r.GET("/", func(c *gee.Context) {
		c.String(http.StatusOK, "Hello Geektutu\n")
	})
	// index out of range for testing Recovery()
	r.GET("/panic", func(c *gee.Context) {
		names := []string{"geektutu"}
		c.String(http.StatusOK, names[100])
	})

	r.Run(":9999")
}

func main() {
	// http.HandleFunc("/", indexHandler)
	// http.HandleFunc("/hellow", hellowhandle)
	// log.Fatal(http.ListenAndServe(":9999", nil))
	// test0()
	test9()
}
